﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FPP.Models;
using FPP.Custom;

namespace FPP.Controllers
{
    public partial class GamesController : BaseController
    {

        //
        // GET: /GameType/

        public ActionResult Index()
        {
            // Default show a list games.

            List<PoolType> lGameTypes = new List<PoolType>();

            using (context)
            {
                lGameTypes = context.PoolTypes.OrderBy("it.Name").ToList();
            }

            return View(lGameTypes);
        }

        //
        // GET: /GameType/Details/5

        public ActionResult Details(int id)
        {
            return View(GetGameById(id));
        }

        /// <summary>
        /// Get Statement of Joining a Game
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// 
        [CustomAuthorize]
        public ActionResult Join(int id)
        {
            PoolType _GameType;
            //var _userCtrl = new UserController();
            //_userCtrl.GetCurrentUser(Url.Action("Join","GameType"));

            using (context)
            {
                _GameType = context.PoolTypes.Single(g => g.Id == id);
            }

            return View(_GameType);
        }

        [CustomAuthorize]
        [HttpPost]
        public ActionResult Join(int id, FormCollection collection)
        {
            var _pool = new Pool();
            var _game = GetGameById(id);
            
            using (context)
            { 
                //Adding the values to the pool object.
                _pool.UserId = GetUserFromLoginToken(provider.SessionId).Id;
                _pool.PoolTypeId = id;
                _pool.FamousPersonId = int.Parse(collection["celebID"].ToString());
                _pool.DateCreated = DateTime.Now;

                context.Pools.AddObject(_pool);

                context.SaveChanges();
            }

            return View(_game);
        }

        private PoolType GetGameById(int id)
        {
            PoolType _GameType;

            using (context)
            {
                _GameType = context.PoolTypes.Single(g => g.Id == id);
            }

            return _GameType;
        }

        //
        // GET: /GameType/Create
        [CustomAuthorize]
        public ActionResult Create()
        {
            if (provider.Role != "Admin") return View(); 
            var _GameType = new PoolType();
            return View(_GameType);
        } 

        //
        // POST: /GameType/Create

        [HttpPost]
        [CustomAuthorize]
        public ActionResult Create(PoolType _GameType, FormCollection collection)
        {
            if (ModelState.IsValid)
            {
                using (context)
                {
                    context.PoolTypes.AddObject(_GameType);
                    context.SaveChanges();

                    return RedirectToAction("Index");
                }
            }

            return View(_GameType);
        }
        
        //
        // GET: /GameType/Edit/5

        [CustomAuthorize]
        public ActionResult Edit(int id)
        {
            PoolType _GameType;

            using (context)
            {
                _GameType = context.PoolTypes.Single(g => g.Id == id);
            }

            return View(_GameType);
        }

        //
        // POST: /GameType/Edit/5

        [HttpPost]
        [CustomAuthorize]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                PoolType _GameType;

                // TODO: Add update logic here

                using (context)
                {
                    _GameType = context.PoolTypes.FirstOrDefault(q => q.Id == id);

                    if (TryUpdateModel(_GameType))
                    {
                        context.SaveChanges();
                        return RedirectToAction("Details",new { Id = id });
                    }
                }

                return View(_GameType);
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /GameType/Delete/5

        [CustomAuthorize]
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /GameType/Delete/5

        [HttpPost]
        [CustomAuthorize]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
